<?php
	$thong_tin_cty = $this->thong_tin_cty;
	$frm_company = $this->form;
?>
<div id="toolbar-box" class="title">
	<div class="top-box">
		<div class="top-box">
			<div class="top-box"></div>
		</div>
	</div>
	<div class="m-box">
		<div class="icon-page title-home"></div>
		<div class="h2">Thông tin công ty</div>
		<div class="clear" style="padding-bottom: 5px;"></div>
	</div>
	<div class="bottom-box">
		<div class="bottom-box">
			<div class="bottom-box"></div>
		</div>
	</div>
</div>
<div class="navbar-inner">
	<table class="info">
		<tbody>
			<tr>
				<th><label class="required">Tên công ty</label></th>
				<td><label><?php echo $thong_tin_cty['cty_ten']; ?></label></td>
			</tr>
			<tr>
				<th><label class="required">Địa chỉ</label></th>
				<td><label><?php echo $thong_tin_cty['cty_dia_chi']; ?></label></td>
			</tr>
			<tr>
				<th><label class="required">Hotline</label></th>
				<td><label><?php echo $thong_tin_cty['cty_hotline']; ?></label></td>
			</tr>
			<tr>
				<th><label class="required">Nick skype</label></th>
				<td><label><?php echo $thong_tin_cty['cty_nick_skype']; ?></label></td>
			</tr>
			<tr>
				<th><label class="required">Nick yahoo</label></th>
				<td><label><?php echo $thong_tin_cty['cty_nick_yahoo']; ?></label></td>
			</tr>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="2" class="row-last">
					<button class="btn btn-primary" data-toggle="modal" onclick="editCompany()" >Chỉnh sửa</button>
				</td>
			</tr>
		</tfoot>
	</table>
</div>

<!-- Chỉnh sửa thông tin công ty -->
<div id="editModal" class="modal customize-modal hide fade" data-backdrop="static" tabindex="-1"
	role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"
			aria-hidden="true">×</button>
		<h3 id="myModalLabel">Chỉnh sửa thông tin công ty</h3>
	</div>
	<div class="modal-body">
		<div id="editalert" class="alert"></div>
		<?php echo '<form id="' . $frm_company->getId() . '" method="' . $frm_company->getMethod() . '" action="" enctype="' . $frm_company->getEnctype() . '" name="' . $frm_company->getName() . '">'; ?>
			<div class="control-group">
				<?php echo $frm_company->cty_ten;?>
			</div>
			<div class="control-group">
				<?php echo $frm_company->cty_dia_chi;?>
			</div>
			<div class="control-group">
				<?php echo $frm_company->cty_hotline;?>
			</div>
			<div class="control-group">
				<?php echo $frm_company->cty_nick_skype;?>
			</div>
			<div class="control-group">
				<?php echo $frm_company->cty_nick_yahoo;?>
			</div>
			<div class="clear"></div>
		<?php echo '</form>'; ?>
	</div>
	<div class="modal-footer">
		<button id="editbtn" class="btn btn-primary">Chỉnh sửa</button>
		<button class="btn" data-dismiss="modal" aria-hidden="true">Hủy</button>
	</div>
</div>
<script type="text/javascript">
	$.fn.serializeObject = function(){
	    var o = {};
	    var a = this.serializeArray();
	    $.each(a, function() {
	        if (o[this.name] !== undefined) {
	            if (!o[this.name].push) {
	                o[this.name] = [o[this.name]];
	            }
	            o[this.name].push(this.value || '');
	        } else {
	            o[this.name] = this.value || '';
	        }
	    });
	    return o;
	};
	
	//show modal
	function editCompany() {
		$('#editalert').hide();
		$('#editModal').modal('show');
	}
	
	//Catch click event of the edit button
	$('#editbtn').click(function(e) {
		editProcess(e);
	});

	//Process edit function
	function editProcess(event) {
		var url = '/admin/company/index';
		var postData = $("#editModal form").serializeObject();
    	$('#editbtn').attr("disabled", "disabled");
		postData.format = "json";
		var isValid = true;
		$('#editModal').find("input").each(function() {
			var inputid = $(this).attr('id');
			isValid = isValid && doValidation('editModal',inputid,'/admin/company/validateform',null);
	    });
		if(isValid) {
			$.ajax({
	            type: 'POST',
	            url: url,
	            async: false,
	            cache: true,
	            data: postData,
	            dataType: postData.format,
	            success: function(data){
            		$('#editalert').html(data.message);
            		$('#editalert').show();
	            	if(data.edit) {
	            		setTimeout(function() {
							$('#editModal').modal('hide'); 
						}, 500);
	            	}
					window.location.reload();
	            }
	        });
		}
    	$('#editbtn').removeAttr("disabled");
	}
	
	function doValidation(modal, id, url, action) {
		$('#' + modal).find('.alert').hide();
	    var data = {};
	    var isValid = true;
	    $('#' + modal).find("input").each(function() {
	        data[$(this).attr('name')] = $(this).val();
	    });
	    if(action){
		    data['action_type'] = 'validate';
		}
	    $.ajax({
            type: 'POST',
            url: url,
            async: false,
            cache: true,
            data: data,
            dataType: 'json',
            success: function(resp){
            	$('#' + modal).find("#" + id).parent().find('.errors').remove();
            	$('#' + modal).find("#" + id).parent().append(getErrorHtml(resp[id], id));
    	    	if(getErrorHtml(resp[id], id))
        	    	isValid = false;
            }
        });
        
	    return isValid;
	}
</script>